package service.impl;

import java.util.List;
import java.util.Map;

import pojo.Food;
import dao.FoodDao;
import dao.impl.FoodDaoImpl;
import dto.ShoppingItem;
import service.ShoppingCartManager;

public class ShoppingCartManagerImpl implements ShoppingCartManager{

	FoodDao dao = new FoodDaoImpl();

	@Override
	public Map<String, ShoppingItem> addShoppingCartItem(
			Map<String, ShoppingItem> shoppingMap, String foodId) {
		if(shoppingMap.containsKey(foodId)){
			///有这个商品，直接货品加1
			ShoppingItem item = shoppingMap.get(foodId);
			item.setCount(item.getCount()+1);
		}else{
			///如果购物车中本没有这样货品，则获取一个货品，放入购物车中
			Food food = dao.searchFood(foodId);
			ShoppingItem si = new ShoppingItem();
			si.setCount(1);
			si.setId(food.getId());
			si.setImg(food.getImg());
			si.setMid(food.getMid());
			si.setName(food.getName());
			si.setPrice(food.getPrice());
			si.setType(food.getType());
			si.setTypeName(food.getTypeName());
			shoppingMap.put(foodId, si);
		}
		return shoppingMap;
	}

	@Override
	public Map<String, ShoppingItem> deleteShoppingCartItem(
			Map<String, ShoppingItem> shoppingMap, String foodId) {
		if(shoppingMap.containsKey(foodId)){
			///有这个商品，直接货品加1
			ShoppingItem item = shoppingMap.get(foodId);
			if(item.getCount()>0)
			item.setCount(item.getCount()-1);
		}
		return shoppingMap;
	}

	@Override
	public void clearShoppingCart(Map<String, ShoppingItem> shoppingMap) {
		shoppingMap.clear();
	}
	
}
